package com.ksyun.cloudhostalarmprocessing.common;

import com.ksyun.cloudhostalarmprocessing.entity.Metrics;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Map;

@Data
@NoArgsConstructor
public class Request {

    //指标名称
    private String metric;

    //标签
    private Map<String,String> tags;

    //时间戳
    private long timestamp;

    //指标值
    private float value;

    public Request(String metric, Map<String, String> tags, int timestamp, float value) throws MyException {
        if(!tags.containsKey("instanceId")){
            throw new MyException("无instanceId");
        }else if(!tags.containsKey("hostname")){
            throw new MyException("无hostname");
        }
        this.metric = metric;
        this.tags = tags;
        this.timestamp = timestamp;
        this.value = value;
    }

    public static Metrics toMetrics(Request request){
        return new Metrics(null, request.metric, request.tags.get("instanceId"), Instant.ofEpochSecond(request.getTimestamp()).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(), request.getValue(), request.tags.get("hostname"));
    }
}
